﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QScrollBar>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    player = new QMediaPlayer(this);
    playList = new QMediaPlaylist(this);
    playList->setPlaybackMode(QMediaPlaylist::Loop);
    player->setPlaylist(playList); //循环模式

    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);    // 禁止最大化按钮
    setFixedSize(this->width(),this->height()); // 禁止拖动窗口大小
    setWindowOpacity(50);
    //this->setWindowFlags(Qt::FramelessWindowHint);
    setui();

    //自定义槽函数
    connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),
            this,SLOT(onStateChanged(QMediaPlayer::State)));

    connect(player,SIGNAL(positionChanged(qint64)),
            this,SLOT(onPositionChanged(qint64)));

    connect(player,SIGNAL(durationChanged(qint64)),
            this, SLOT(onDurationChanged(qint64)));

    connect(playList,SIGNAL(currentIndexChanged(int)),
            this, SLOT(onPlaylistChanged(int)));



    QString fileName="C:/Users/ZhaoJingyi/Desktop/musictext";
    playPath=fileName;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onStateChanged(QMediaPlayer::State state)
{
   //播放器状态变化，更新按钮状态
    ui->btnPlay->setEnabled(!(state == QMediaPlayer::PlayingState));
    ui->btnPause->setEnabled(state == QMediaPlayer::PlayingState);
    ui->btnStop->setEnabled(state == QMediaPlayer::PlayingState);
}

void MainWindow::onPlaylistChanged(int position)
{
    //播放列表变化，更新当前播放文件名显示
    ui->listWidget->setCurrentRow(position);
    QListWidgetItem *item = ui->listWidget->currentItem();
    if(item)
    {
        ui->LabCurMedia->setText(item->text()); //显示当前的的音乐
    }
}

void MainWindow::onDurationChanged(qint64 duration)
{
   //文件时长变化，更新进度显示
    ui->sliderPosition->setMaximum(duration);

    int secs =int (duration/1000); //秒
    int mins =secs / 60; //分钟
    secs=secs % 60;//余数秒
    durationTime = QString::asprintf("%d:%d",mins,secs);
    ui->LabRatio->setText(positionTime+"/"+durationTime);
}

void MainWindow::onPositionChanged(qint64 position)
{
   //当前文件播放位置变化，更新进度显示
    if (ui->sliderPosition->isSliderDown())
        return;

    ui->sliderPosition->setSliderPosition(position);
    int secs =int (position/1000); //秒
    int mins =secs / 60; //分钟
    secs=secs % 60;//余数秒
    positionTime = QString::asprintf("%d:%d",mins,secs);
    ui->LabRatio->setText(positionTime+"/"+durationTime);
}


void MainWindow::on_btnAdd_clicked()
{
    //添加文件
    QString curPath = QDir::homePath(); //获取系统当前目录
    QString dlgTitle = "选择音频文件"; //对话框标题
    QString filter = "音频文件(*.mp3 *.wav *.wma);;mp3文件(*.mp3);;wav文件(*.wav);;wma文件(*.wma);;所有文件(*.*)"; //文件过滤
    QStringList fileList = QFileDialog::getOpenFileNames(this,dlgTitle,curPath,filter); //添加多个

    if(filter.count() < 1)
    {
        return;
    }

    for (int i = 0;i<fileList.count();i++) {
        QString aFile = fileList.at(i);
        playList->addMedia(QUrl::fromLocalFile(aFile));//添加文件

        QFileInfo fileInfo(aFile);
        ui->listWidget->addItem(new QListWidgetItem(QIcon(":/picture/music.png"),fileInfo.fileName())); //添加到界面文件列表
       // playPath=aFile;
    }

    if(player->state()!= QMediaPlayer::PlayingState)
    {
        playList->setCurrentIndex(0);
    }
    player->play();

}

void MainWindow::on_btnRemove_clicked()
{
    //移除一个文件
    int pos = ui->listWidget->currentRow(); //获取当前选中的行
    QListWidgetItem *item = ui->listWidget->takeItem(pos);
    delete item;

    if(playList->currentIndex()==pos)  //当删除的是当前播放的音乐
    {
        int nextPos=0;
        if (pos>=1)
            nextPos=pos-1;
        playList->removeMedia(pos);
        if(ui->listWidget->count() > 0)
        {
            playList->setCurrentIndex(nextPos);
            onPlaylistChanged(nextPos);
        }
        else
        {
            player->stop();
            ui->LabCurMedia->setText("无曲目");
        }
    }
    else
    {
        playList->removeMedia(pos);
    }
}

void MainWindow::on_btnClear_clicked()
{
    //清空列表
    playList->clear();
    ui->listWidget->clear();
    player->stop();
}


void MainWindow::on_listWidget_doubleClicked(const QModelIndex &index)
{
    int rowNo = index.row(); //获取当前行
    playList->setCurrentIndex(rowNo); //选择当前双击的位置
    player->play();

    QListWidgetItem *item =ui->listWidget->currentItem();
    QString name=item->text().left(item->text().lastIndexOf("."));
    //qDebug() << playPath <<name ;
    if(!lyrics.readLyricsFile(playPath+ "/" + name+ ".lrc")){
            ui->labLyrics1->setText("未检测到歌词文件");
            ui->labLyrics2->setText("请检查歌词文件是否存在");
    }
        lyricsID = 0;
}

//声音进度条设置
void MainWindow::on_sliderVolumn_valueChanged(int value)
{
    player->setVolume(value);
}

void MainWindow::on_btnSound_clicked()
{
    //静音控制
    bool mute = player->isMuted();
    player->setMuted(!mute);
    if(mute)
    {
        ui->btnSound->setIcon(QIcon(":/picture/sound.png"));
    }
    else
    {
        ui->btnSound->setIcon(QIcon(":/picture/nosound.png"));
    }
}


void MainWindow::on_btnPlay_clicked()
{
    //播放
    if(playList->currentIndex() < 0)
    {
        playList->setCurrentIndex(0);
    }

    player->play();
}


void MainWindow::on_btnPause_clicked()
{
    //暂停播放
    player->pause();
}

void MainWindow::on_btnStop_clicked()
{
    //停止播放
    player->stop();
}

void MainWindow::on_sliderPosition_valueChanged(int value)
{
    //文件进度调控
    player->setPosition(value);
}

void MainWindow::on_btnPrevious_clicked()
{
    //前一文件
    playList->previous();
}

void MainWindow::on_btnNext_clicked()
{
    //下一文件
    playList->next();
}

//播放模式改变
void MainWindow::PlaybackModel(QMediaPlaylist::PlaybackMode model)
{
    if(model==QMediaPlaylist::Loop)
    {
        ui->btnPlayModel->setIcon(QIcon(":/picture/shunxu2.png"));
        ui->btnPlayModel->setToolTip(("顺序播放"));
    }else if(model==QMediaPlaylist::Random){
        ui->btnPlayModel->setIcon(QIcon(":/picture/suiji.png"));
        ui->btnPlayModel->setToolTip(("随机播放"));
    }else if(model==QMediaPlaylist::CurrentItemInLoop){
        ui->btnPlayModel->setIcon(QIcon(":/picture/xunhuan.png"));
        ui->btnPlayModel->setToolTip(("单曲循环"));
    }
}

//播放模式设置
void MainWindow::on_btnPlayModel_clicked()
{
    if(playList->playbackMode()==QMediaPlaylist::Loop)
    {

        ui->btnPlayModel->setIcon(QIcon(":/picture/suiji.png"));
        ui->btnPlayModel->setToolTip(("随机播放"));
        playList->setPlaybackMode(QMediaPlaylist::Random);

    }
    else if(playList->playbackMode()==QMediaPlaylist::Random)
    {
        ui->btnPlayModel->setIcon(QIcon(":/picture/xunhuan.png"));
        ui->btnPlayModel->setToolTip(("单曲循环"));
        playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);


    }
    else if(playList->playbackMode()==QMediaPlaylist::CurrentItemInLoop)
    {
        ui->btnPlayModel->setIcon(QIcon(":/picture/shunxu2.png"));
        ui->btnPlayModel->setToolTip(("顺序播放"));
        playList->setPlaybackMode(QMediaPlaylist::Loop);

    }
}
//歌词相关
void   MainWindow::setPlayTime()
{
    if(!lyrics.getListLyricsTime().empty()&& player->position()>=lyrics.getListLyricsTime().at(lyricsID)&&lyricsID<lyrics.getListLyricsTime().size()-1)
    {
        ui->labLyrics1->setText(lyrics.getListLyricsText().at(lyricsID));
        ui->labLyrics2->setText(lyrics.getListLyricsText().at(lyricsID+1));
        lyricsID++;
    }
    //ui->lblPlayTime->setText(settime(m_mediaPlayer.position()));
}

//初始化UI
void MainWindow::setui()
{
    //对ui按钮设置图标以及划过显示
    ui->btnPlay->setIcon(QIcon(":/images/620.bmp"));
    ui->btnPlay->setToolTip(("播放"));

    ui->btnPause->setIcon(QIcon(":/images/622.bmp"));
    ui->btnPause->setToolTip(("暂停"));

    ui->btnStop->setIcon(QIcon(":/images/624.bmp"));
    ui->btnStop->setToolTip(("重新开始"));

    ui->btnPrevious->setIcon(QIcon(":/images/616.bmp"));
    ui->btnPrevious->setToolTip(("上一曲"));

    ui->btnNext->setIcon(QIcon(":/images/630.bmp"));
    ui->btnNext->setToolTip(("下一曲"));

    ui->btnSound->setIcon(QIcon(":/picture/sound.png"));
    ui->btnSound->setToolTip(("静音"));

    ui->label->setPixmap(QPixmap(":/picture/label.png"));
    ui->label->setScaledContents(true);
    ui->btnAdd->setIcon(QIcon(":/images/316.bmp"));
    ui->btnAdd->setToolTip(("添加歌曲"));

    ui->btnRemove->setIcon(QIcon(":/images/318.bmp"));
    ui->btnRemove->setToolTip(("从列表中删除"));

    ui->btnClear->setIcon(QIcon(":/picture/clear2.png"));
    ui->btnClear->setToolTip(("清除列表"));
    ui->btnPlayModel->setIcon(QIcon(":/picture/shunxu2.png"));

    //对标签显示进行设置
    QFont ft1;
    QFont ft2;
    ft1.setPointSize(12);
    ft2.setPointSize(10);
    ui->LabCurMedia->setFont(ft1);
    ui->LabRatio->setFont(ft2);

    QPalette pa1;
    QPalette pa2;
    pa1.setColor(QPalette::WindowText,Qt::red);
    ui->LabCurMedia->setPalette(pa1);
    pa2.setColor(QPalette::WindowText,Qt::blue);
    ui->LabRatio->setPalette(pa2);

    ui->listWidget->setAutoFillBackground(true);
    QPalette pa3;
    pa3.setColor(QPalette::Background,QColor(192,253,123,20));
    ui->listWidget->setPalette(pa3);

    //设置ui按钮的详细参数
    ui->listWidget->setStyleSheet("background-color: rgb(0, 0, 0 , 0)");
    ui->btnAdd->setStyleSheet("background-color: rgb(255, 255, 255)");
    ui->btnAdd->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnAdd->setFlat(true);
    ui->btnRemove->setStyleSheet("background-color: rgb(255, 255, 255)");
    ui->btnRemove->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnRemove->setFlat(true);
    ui->btnClear->setStyleSheet("background-color: rgb(255, 255, 255)");
    ui->btnClear->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnClear->setFlat(true);
    ui->btnPlay->setStyleSheet("background-color: rgb(255, 255, 255)");
    ui->btnPlay->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnPlay->setFlat(true);
    ui->btnPause->setStyleSheet("background-color: rgb(255, 255, 255)");
    ui->btnPause->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnPause->setFlat(true);
    ui->btnStop->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnStop->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnStop->setFlat(true);
    ui->btnPrevious->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnPrevious->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnPrevious->setFlat(true);
    ui->btnNext->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnNext->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnNext->setFlat(true);
    ui->btnPlayModel->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnPlayModel->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");
    ui->btnPlayModel->setFlat(true);

    QString listWidgetStyle = "QScrollBar:vertical"
                                      "{"
                                          "width:8px;"
                                          "background-color:transparent;"
                                          "margin:0px,0px,0px,0px;"
                                         " padding-top:12px;   /*上预留位置*/"
                                         " padding-bottom:12px;    /*下预留位置*/"
                                      "}"


                                     "QScrollBar::handle:vertical"
                                     " {"
                                     "     width:8px;"
                                     "     background-color:rgba(255,255,255,0.2);"
                                     "     border-radius:4px;"
                                     "     min-height:20px;"
                                     " }"


                                     "QScrollBar::handle:vertical:hover"
                                     "{"
                                     "    width:9px;"
                                     "    background-color:rgba(255,255,255,0.5);"
                                     "    border-radius:4px;"
                                     "    min-height:20;"
                                     "}"

                                     "QScrollBar::add-line:vertical"
                                     "{"
                                     "     height:12px;"
                                     "     width:10px;"
                                     "     border-image:url(:/selectfile/scroll/3.png);"
                                     "     subcontrol-position:bottom;"
                                     "}"

                                     "QScrollBar::sub-line:vertical"
                                     "{"
                                     "     height:12px;"
                                     "     width:10px;"
                                     "     border-image:url(:/selectfile/scroll/1.png);"
                                     "     subcontrol-position:top;"
                                     "}"
                                     "QScrollBar::add-line:vertical:hover"
                                     "{"
                                     "     height:12px;"
                                     "     width:10px;"
                                     "     border-image:url(:/selectfile/scroll/4.png);"
                                     "     subcontrol-position:bottom;"
                                     " }"


                                     " QScrollBar::sub-line:vertical:hover"
                                     " {"
                                     "     height:12px;"
                                     "     width:10px;"
                                     "     border-image:url(:/selectfile/scroll/2.png);"
                                     "     subcontrol-position:top;"
                                     " }"

                                     " QScrollBar::add-page:vertical,"
                                     "QScrollBar::sub-page:vertical"
                                     " {"
                                     "     background-color:transparent;"
                                     "     border-radius:4px;"
                                     "}";

        ui->listWidget->verticalScrollBar()->setStyleSheet(listWidgetStyle);

}
